package org.tity.test.module.user.controller;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.tity.backends.api.annotations.API;
import org.tity.backends.api.annotations.ErrorCode;
import org.tity.backends.api.annotations.ErrorCodes;
import org.tity.backends.api.annotations.Modifies;
import org.tity.backends.api.annotations.Modify;
import org.tity.backends.api.annotations.Param;
import org.tity.backends.api.annotations.Params;
import org.tity.backends.api.annotations.ReturnDatas;
import org.tity.backends.api.annotations.VType;
import org.tity.backends.transfer.Parameter;
import org.tity.ends.error.CommonError;
import org.tity.ends.transfer.Result;
import org.tity.sql.dao.DAO;
import org.tity.test.entity.raw.sys.User;
import org.tity.test.module.user.service.UserService;

import com.alicp.jetcache.anno.Cached;

@RestController
@RequestMapping(value="/user")
public class UserController {
	
	@Autowired
	private DAO dao;
	
	@Autowired
	private UserService service=null;
	
	@RequestMapping(value="/list")
	@Cached(name="user.list", expire = 24,timeUnit=TimeUnit.SECONDS)
	public @ResponseBody Result list(@RequestBody Parameter ps) {
//		UserParam
		
		//从参数中提取实体
		User us=ps.extractEntity(User.class);
		
//		Where wh=ps.extractWhere(User.class);
		
		//三种不同的实体保存
		dao.saveEntityAllFields(us); //保存所有字段
		dao.saveEntityDirtyFields(us); //保存修改过的字段
		dao.saveEntityNotNullFields(us); //保存非空字段
		
		
		System.out.println(us.getId()+":"+us.getAccount());
		service.getUsers(us);
		return Result.SUCCESS();
	}
	
	@RequestMapping(value="/save")
//	@Cached(name="user.list", expire = 24,timeUnit=TimeUnit.SECONDS)
	public @ResponseBody Result save(@RequestBody Parameter ps) {
		

		
		User us=ps.extractEntity(User.class);
		System.out.println(us.getId()+":"+us.getAccount());
		service.getUsers(us);
		return Result.SUCCESS();
	}
	
	
//	@RequestMapping("/wf-demo") 
//	public @ResponseBody Mono<Result> handleGetUsers(@RequestBody Parameter ps) {
//		
//		(new Exception()).printStackTrace();
//		 
//		return  Mono.just(Result.SUCCESS());
//	}
	
	
	@API(name="清除缓存")
	@RequestMapping(value="clear-cache")
	@Modifies({
		@Modify(author="李方捷",time="20180822",note="创建")
	})
	@Params({
		@Param(key="cache_name",name="缓存名称",vType={VType.REQUIRE, VType.MAX_LENGTH + "10"}),
		
		@Param(key="var_equals",name="地区",vType={VType.EQUALS+"NingBo"}),
		
		@Param(key="var_minlen",name="最小长度",vType={VType.MIN_LENGTH+"10"}),
		@Param(key="var_maxlen",name="最大长度",vType={VType.MAX_LENGTH+"10"}),
		
		@Param(key="var_lt",name="采购价",vType={VType.LESS_THAN+"10"}),
		@Param(key="var_gt",name="零售价",vType={VType.GREATER_THAN+"10"}),
		
		@Param(key="var_nlt",name="生日",vType={VType.NOT_LESS_THAN+"2018-10-10"}),
		@Param(key="var_ngt",name="入职日期",vType={VType.NOT_GREATER_THAN+"2019-11-12 12:15"}),
		
		@Param(key="var_number",name="数字参数",vType={VType.TYPE_NUMBER}),
		@Param(key="var_int",name="整型参数",vType={VType.TYPE_INTEGER}),
		@Param(key="var_date",name="日期参数",vType={VType.TYPE_DATE}),
		@Param(key="var_bool",name="逻辑参数",vType={VType.TYPE_BOOL}),
		
		@Param(key="var_mobile",name="手机号码",vType={VType.MOBILE}),
		@Param(key="var_email",name="邮箱地址",vType={VType.EMAIL}),
		@Param(key="var_idcard",name="身份证",vType={VType.ID_CARD}),
		@Param(key="var_url",name="企业网站",vType={VType.URL}),
		
		@Param(key="begin_time",name="开始时间",vType={VType.REQUIRE,VType.LESS_THAN+"@end_time",VType.GREATER_THAN+"@today"}),
		@Param(key="end_time",name="结束时间",vType={VType.REQUIRE,VType.GREATER_THAN+"@begin_time"}),
		
		@Param(key="sex",name="性别",vType={VType.LIMIT_VALUES+"F,M"}),
		//路径
		@Param(key="users.0.name",name="姓名",vType={VType.TYPE_STRING}),
		@Param(key="ends",name="端口类型",vType={VType.LIMIT_ENUM+"org.tity.ends.deploy.EndsType"}),
		
		@Param(key="notify",name="通知其它节点",note="是否通知集群中的其他节点清除缓存",vType={}, defaultValue="Y")
	})
	@ReturnDatas({
	})
	@ErrorCodes({
		@ErrorCode(code=CommonError.SUCCESS,detail=""),
		@ErrorCode(code=CommonError.FALIURE,detail="")
	})
	public @ResponseBody Result clearCache(@RequestBody Parameter ps) {
		String name = ps.getString("cache_name");
		 
		Boolean notifyOthers=ps.getBoolean("notify");
		if(notifyOthers)
		{
			 
		}
		return Result.SUCCESS();
	}
}
